package is.Hazir.SatGPS;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;


public class ProximityReceiver extends BroadcastReceiver {
	
	private class CustomPhoneStateListener extends PhoneStateListener {
		Context context;
		public CustomPhoneStateListener(Context context) {
			super();
			this.context = context;
		}
	}

	// Tilviksbreytur fyrir titringin a snjallsimanum =>
	protected long[] morsaR    = {180, 180, 180, 640, 180, 180};
	protected long[] morsaK    = {180, 640, 180, 180, 180, 640};
	Context context;
	CustomPhoneStateListener listener;

	@Override
	public void onReceive(Context context, Intent intent) {
		AudioManager audiomanager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
		this.context = context;
		final Context final_context = context;
		final TelephonyManager tmanager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

		if (intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false)) {
			try
			{
				// "R" vibringu finnst ef ad notandinn fer inn a vistad svaedi, sem
				// heldur simanum stilltum (hazir), svo ad hann hringi ekki a medan.
				// Morsa "R" = . _ . med vibringu:
				((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(morsaR, -1);
			}
			catch (Exception e)
			{
				Toast.makeText(context, "Villa kom upp: Titra ekki!", Toast.LENGTH_LONG).show();
			} // try catch

			if (intent.getIntExtra("silent", 0) > 0) {
				audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			}
			if (intent.getIntExtra("sms", 0) > 0) {
				final String smsText = intent.getStringExtra("smsText");
				Toast.makeText(context, smsText, Toast.LENGTH_LONG).show();
				//
				// Senda SMS ef ad hringt er a medan ad siminn er innan "fridar - svaedisins",
				// svo framanlega ad siminn er ekki a tali (IDLE "idinn") eda ekki sje verid
				// ad hringja i einhvern (OFFHOOK).
				listener = new CustomPhoneStateListener(context) {
					@Override
					public void onCallStateChanged(int state, String incomingNumber) {
						switch (state) {
						case TelephonyManager.CALL_STATE_IDLE:
							break;
						case TelephonyManager.CALL_STATE_OFFHOOK:
							break;
						case TelephonyManager.CALL_STATE_RINGING:
							try {
								Class c = Class.forName(tmanager.getClass().getName());
								Method m = c.getDeclaredMethod("getITelephony");
								m.setAccessible(true);
								ITelephony telephonyService = (ITelephony) m.invoke(tmanager);
								telephonyService.endCall();
								SendSMS(incomingNumber, smsText);
							}
							catch (Exception e) {
								Toast.makeText(final_context, "Villa kom upp", Toast.LENGTH_LONG).show();
								e.printStackTrace();
								Log.i("is.Hazir.ProximityReceiver", "Error object: " + e);
							}
							
							break;
						}
					}
				};
				tmanager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
			}
		}
		else // EKKI => if (intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false))
		{
			try
			{
				// "K" vibringu finnst ef ad "fridar - svaedid" er yfirgefid og =>
				// Morsa "K" = . _ . med vibringu:
				((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(morsaK, -1);
			}
			catch (Exception e)
			{
				Toast.makeText(context, "Villa kom upp: Titra ekki!",
						Toast.LENGTH_LONG).show();
			} // try catch
			//
			// um leid verdur kveikt a hringinguna, ef ad sloekkt hefdi verid a hringingunni og =>
			if (intent.getIntExtra("silent", 0) > 0) {
				audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			}
			// enginn SMS tharf ad senda i bili, vegna thess ad siminn hringir.
			if (intent.getIntExtra("sms", 0) > 0) {
				tmanager.listen(listener, PhoneStateListener.LISTEN_NONE);
			}

		}

	}

	protected static void SendSMS(String incomingNumber, String message) {
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage("00354"+incomingNumber, null, message, null, null);
	}	
}